package com.hpu.imgdown.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*import javax.servlet.jsp.PageContext;*/

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.hpu.imgdown.util.DownsUtil;

/**
 * 
 * @ClassName: ImgDownServlet 
 * @Description: TODO
 * @author: zch
 * @verrsion: v.10
 * @date: 2018?7?6? ??12:14:44
 */
public class ImgDownServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	/*private static PageContext pageContext;*/
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ImgDownServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		System.out.println("init()?????????????");
	}

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
		System.out.println("destroy()??????????????servlet?????????");
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("service()?????init()?????󱻵????");
		super.service(request, response);
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("doGet()???????????̨ҳ??????????et????");
		doPost(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("doPost()???????????̨ҳ??????????ost????");
		request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
		String url = request.getParameter("url");
		String encoding = "utf-8";
		String html = DownsUtil.getHtmlResourceByUrl(url, encoding);//????rl?ַ???ҳ??뼯????ҳ??????	
		request.getSession().setAttribute("html_source", html);
		
		/**
		 * ???Դ???,Jsoup?һ??ava??TML????
		 * ???html?????String?ʽ?????????????parse????ֱ?ӽ??ΪDocument??
         * Document doc = Jsoup.parse(html);
         * ??url?ʽ?Ļ??????connect????ֱ?ӻ??html?????
         * Document doc = Jsoup.connect("http://blog.csdn.net/roy_70").get();
		 */
		Document document = Jsoup.parse(html);
		
		/**
		 * ?????ͼƬ?ĵ??,element???ɷ֣?Ҫ???Ԫ???
		 * ??????select?????ֱ?Ӽ̳??bject????ʵ??Ľӿ?
		 * ?Cloneable, Iterable<Element>, Collection<Element>, List<Element> 
		 */
		Elements elements = document.getElementsByTag("img");
		/**
		 * Element???ode??????࣬??ͬ?ʵ???loneable?ӿڣ??????һ????????????
		 * ??ӽڵ????tmlԪ??????Ԫ????????????ݣ?
		 * ??Ա???ڵ??????Բ??tml??
		 */
		List<String> list = new ArrayList<String>();
		for(Element element :elements) {
			String imgSrc = element.attr("src");//???img??????src??ֵ
			if(!"".equals(imgSrc)&&(imgSrc.startsWith("http://")||imgSrc.startsWith("https://"))) {
				list.add(imgSrc + "\n");
			}
			
		}
        request.getSession().setAttribute("list", list);
		//ת?????һ??ҳ????????request?????????
		request.getRequestDispatcher("show.jsp").forward(request, response);
	}


}
